define(function(require, exports, module) {
    var isIE=!!window.ActiveXObject;
    var isIE6=isIE&&!window.XMLHttpRequest;
    var isIE8=isIE&&!!document.documentMode;
    var isIE7=isIE&&!isIE6&&!isIE8;

    //jquery缓动动画扩展
    jQuery.easing['jswing'] = jQuery.easing['swing'];
    jQuery.extend( jQuery.easing,
    {
        def: 'easeOutQuad',
        easeOutQuad: function (x, t, b, c, d) {
            return -c *(t/=d)*(t-2) + b;
        }
    });

    var pCommon = {};

    //tab切换
    pCommon.tabs = {
        //对外提供重置全局参数
        opts: {},
        init:function(selector, options){
            var tabs = function(sel, opt){
                var opts = $.extend({}, this.defaults, opt);
                var jcon = $(sel),
                    jtabs = jcon.find(opts.tabBox).find(opts.tabItem),
                    jcons = jcon.find(opts.conBox).find(opts.conItem);
                jtabs.bind(opts.ev, function(){
                    var i = $(this).index();
                    jtabs.removeClass(opts.tabOnClass).eq(i).addClass(opts.tabOnClass);
                    jcons.hide().eq(i).show();
                });
            };

            tabs.prototype.defaults = {
                    //tab标签包裹元素
                    tabBox: ".fn_tab",
                    //tab标签元素
                    tabItem: ".fn_tab_item",
                    //内容包裹元素
                    conBox: ".fn_con",
                    //内容元素
                    conItem: ".fn_con_item",
                    //tab选中样式名
                    tabOnClass: "on",
                    //触发事件
                    ev: "click"
                };
            tabs.prototype.defaults = $.extend({}, tabs.prototype.defaults, pCommon.tabs.opts);
            return new tabs(selector, options);
        }

    };

    //图片轮播
    pCommon.slider = {
        //对外提供重置全局参数
        opts: {},
        init: function(selector, options){
            var slider = function(sel, opt){
                var me = this;

                //参数
                var opts = $.extend({}, me.defaults, opt);
                var jcon = $(sel),
                    //图片索引状态按钮
                    jbtns = jcon.find(opts.btnBox).find(opts.btnItem),
                    //图片显示框
                    jconBox = jcon.find(opts.conBox),
                    //图片滚动框
                    jconInner = jcon.find(opts.conInner),
                    //图片列表
                    jcons = jcon.find(opts.conBox).find(opts.conItem),
                    //上一页
                    jpreBtn = jcon.find(opts.btnPre),
                    //下一页
                    jnextBtn = jcon.find(opts.btnNext),
                    //len:图片个数
                    //nlen:克隆首尾图片后个数，用于循环展示
                    //curI当前索引值
                    //unitW、unitH显示框宽高
                    len = jcons.length, nlen = len + 2, curI = 0, unitW = jconBox.width(), unitH = jconBox.height();
                var go2con = null, timer = null, timer2;

                //初始化dom操作
                if (opts.type == "h" || opts.type == "v") {
                    jconInner.append(jcons.eq(0).clone());
                    jconInner.prepend(jcons.eq(len-1).clone());
                    jcons = jcon.find(opts.conBox).find(opts.conItem);
                }
                jcons.css({width: unitW + "px", height: unitH + "px", overflow: "hidden"});


                //向后循环返回初始位置
                function reStart(){
                    if (opts.type == "h") {
                        jconInner.css({left: 0});
                    }else if (opts.type == "v") {
                        jconInner.css({top: 0});
                    }
                }
                //向前循环返回末尾位置
                function reEnd(){
                    if (opts.type == "h") {
                        jconInner.css({left: -(len+1)*unitW});
                    }else if (opts.type == "v") {
                        jconInner.css({top: -(len+1)*unitH});
                    }
                };
                if (opts.type == "h") {
                    jconInner.css({left:-unitW + "px", width: nlen * unitW + "px", height: unitH + "px"});
                    go2con = function(index){
                        var l = - (index * unitW) - unitW;
                        jbtns.removeClass(opts.tabOnClass).eq(index).addClass(opts.tabOnClass);
                        jconInner.stop().animate({left: l + "px"}, 500, 'easeOutQuad');
                        curI = index;
                    };
                }else if (opts.type == "v") {
                    jconInner.css({top:-unitH + "px"});
                    go2con = function(index){
                        var l = - (index * unitH) - unitH;
                        jbtns.removeClass(opts.tabOnClass).eq(index).addClass(opts.tabOnClass);
                        jconInner.stop().animate({top: l + "px"}, 500, 'easeOutQuad');
                        curI = index;
                    };
                }else if(opts.type == "a"){
                    jcons.css({position: "absolute"}).hide().eq(curI).show();
                    go2con = function(index){
                        if (curI == index) {
                            return;
                        }
                        jbtns.removeClass(opts.tabOnClass).eq(index).addClass(opts.tabOnClass);
                        jcons.stop().hide().eq(index).fadeIn(300);
                        jcons.eq(curI).show().fadeOut(300);
                        curI = index;
                    };
                }else if(opts.type == "n"){
                    go2con = function(index){
                        var l = - (index * unitH);
                        jbtns.removeClass(opts.tabOnClass).eq(index).addClass(opts.tabOnClass);
                        jconInner.css({top: l + "px"});
                        curI = index;
                    };
                }
                jbtns.bind(opts.ev, function(){
                    var i = $(this).index();
                    go2con(i);
                });
                jpreBtn.click(function(){
                    var i = curI - 1;
                    if (i < 0) {
                        i = len - 1;
                        reEnd();
                    }
                    go2con(i);
                });
                jnextBtn.click(function(){
                    var i = curI + 1;
                    if (i >= len) {
                        i = 0;
                        reStart();
                    }
                    go2con(i);
                });

                if (opts.auto) {
                    jcon.bind("mouseover", function(){
                        clearInterval(timer);
                        timer = null;
                    }).bind("mouseout", function(){
                        clearInterval(timer);
                        timer = null;
                        timer = setInterval(function(){
                            var i = curI + 1;
                            if (i >= len) {
                                i = 0;
                            }
                            go2con(i);
                        }, opts.time);
                    }).trigger("mouseout");
                }
            };

            slider.prototype.defaults = {
                //按钮包裹元素
                btnBox: ".fn_btnbox",
                //按钮元素
                btnItem: ".fn_btn_item",
                //上一页
                btnPre: ".fn_pre",
                //下一页
                btnNext: ".fn_next",
                //内容包裹元素
                conBox: ".fn_con",
                //内容包裹滚动元素
                conInner: ".fn_con_inner",
                //内容元素
                conItem: ".fn_con_item",
                //tab选中样式名
                tabOnClass: "on",
                //切换类型v(纵向)\h(横向)\a(透明度)\n(无效果)
                type: "h",
                //触发事件
                ev: "mouseover",
                auto: true,
                time: 4000
            };
            slider.prototype.defaults = $.extend({}, slider.prototype.defaults, pCommon.slider.opts);
            return new slider(selector, options);
        }
    };


    //漂浮层
    pCommon.floatDiv = {
        init: function(selector, options) {
            var defaults = {
                //type
                //1：右侧
                //2：左侧
                type: 1,
                //主体宽度
                mainW: 960,
                //相对偏移值
                posX: 10,
                posY: 10
            };

            var opts = $.extend({}, defaults, options);
            var jbtn = $(selector);
            var scrollT = 0, clientH, clientW,
                btnY = 0,
                btnX = 0,
                lesW = 0,
                btnH = jbtn.height();
            btnW = jbtn.width();

            function setY() {
                if (isIE6) {
                    scrollT = document.documentElement.scrollTop || document.body.scrollTop;
                    btnY = scrollT + clientH - opts.posY - btnH;
                    jbtn.css({top: btnY  + "px"});
                }else{
                    btnY = opts.posY;
                    jbtn.css({bottom: btnY  + "px"});
                }
            }
            function resetPos() {
                clientH = document.documentElement.clientHeight;
                clientW = document.documentElement.clientWidth;
                lesW = clientW - opts.mainW;
                if (lesW > btnW + opts.posX) {
                    btnX = (lesW/2) - btnW - opts.posX;
                }else{
                    btnX = opts.posX;
                }
                if (opts.type == 1) {
                    jbtn.css({right: btnX, left: ""});
                }else if(opts.type == 2){
                    jbtn.css({right: "", left: btnX});
                }
                setY();
            }
            if (jbtn.length>0) {
                if (isIE6) {
                    jbtn.css({position: "absolute"});
                }else{
                    jbtn.css({position: "fixed"});
                }
                resetPos();

                $(window).bind("scroll", function () {
                    setY();
                });

                $(window).bind("resize",function(){
                    resetPos();
                });
            }
        }
    };


    //返回顶部
    pCommon.back2top = function(selector, options) {
        pCommon.floatDiv.init(selector, options);
    };

    //按钮锁定倒计时
    //按钮处理方法调用不能写在html中
    pCommon.timeBtn = {
        //缓存对象
        store:{},
        init:function(id, options){
            var obj = null;
            var lockBtn = function(_id, opt){
                var me = this;
                var defaults = {
                    //按钮上锁样式
                    lockClass: "locked",
                    //倒计时时间
                    time: 60,
                    //锁定后的文字
                    replaceTxt: null
                };

                var opts = $.extend({}, defaults, opt);
                var jbtn = $("#" + _id);
                var jbtnClone = jbtn.clone().attr("id","");
                var jtxtCon = jbtnClone.find(".fn_txt");
                var timer = null;
                me.isLockInit = false;
                me.isLock = false;
                //对象方法，用于强制解锁按钮
                me.unlock = function(){
                    if (me.isLockInit) {
                        me.isLock = false;
                        jbtn.show();
                        jbtnClone.hide();
                        clearInterval(timer);
                        timer = null;
                    }
                };
                me.lock = function(){
                    var t = opts.time;
                    jtxtCon.html(t);
                    if (!me.isLock) {
                        me.isLock = true;
                        if (!me.isLockInit) {
                            me.isLockInit = true;
                            jbtn.hide().after(jbtnClone);
                            jbtnClone.addClass(opts.lockClass);
                        }else{
                            jbtn.hide();
                            jbtnClone.show();
                        }
                        if (!opts.replaceTxt) {
                            timer = setInterval(function(){
                                if (t<0) {
                                    me.unlock();
                                }
                                jtxtCon.html(t--);
                            }, 1000);
                        }else{
                            jbtnClone.html(opts.replaceTxt);
                        }
                    }
                };
            };
            //初始化返回新对象并存入缓存，下次初始化同一按钮则返回缓存对象
            if (!pCommon.timeBtn.store[id]) {
                obj = new lockBtn(id, options);
                pCommon.timeBtn.store[id] = obj;
                return obj;
            }else{
                return pCommon.timeBtn.store[id];
            }
        }
    };

    // 暴露对应接口
    module.exports = pCommon;
});
